package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weijianhuawen.wjblog.common.config.InsertBatchMapper;
import com.weijianhuawen.wjblog.common.domain.dos.WikiCatalogDo;
import com.weijianhuawen.wjblog.common.enums.WikiCatalogLevelEnum;

import java.util.List;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-11 13:36
 */
public interface WikiCatalogMapper extends InsertBatchMapper<WikiCatalogDo> {
    // 通过知识库id获取该知识库下的所有目录
    default List<WikiCatalogDo> selectListByWikiId(Long wikiId) {
        return selectList(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getWikiId, wikiId));
    }
    // 通过wikiId删除 所关联的目录
    default Integer deleteByWikiId(Long wikiId) {
        return delete(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getWikiId, wikiId));
    }
    // 通过文章id查询所有关联目录
    default List<WikiCatalogDo> selectListByArticleId(Long articleId) {
        return selectList(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getArticleId, articleId));
    }

    // 查询知识库中的第一篇文章
    default WikiCatalogDo selectFirstArticleId(Long wikiId) {
        return selectOne(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getWikiId, wikiId)
                .eq(WikiCatalogDo::getLevel, WikiCatalogLevelEnum.TWO.getValue())
                .isNotNull(WikiCatalogDo::getArticleId)
                .orderByAsc(WikiCatalogDo::getId)
                .last("LIMIT 1"));
    }

    // 根据知识库id和文章id筛选目录
    default WikiCatalogDo selectCatalogByWikiIdAndArticleId(Long wikiId, Long articleId) {
        return selectOne(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getWikiId, wikiId)
                .eq(WikiCatalogDo::getArticleId, articleId));
    }

    // 查询上一篇文章目录
    default WikiCatalogDo selectPreCatalog(Long wikiId, Long parentId,  Integer curSort) {
        return selectOne(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getWikiId, wikiId)
                .eq(WikiCatalogDo::getParentId, parentId)
                .isNotNull(WikiCatalogDo::getArticleId)
                .orderByDesc(WikiCatalogDo::getSort)
                .lt(WikiCatalogDo::getSort, curSort)
                .last("LIMIT 1"));
    }

    // 查询下一篇文章目录
    default WikiCatalogDo selectNextCatalog(Long wikiId, Long parentId, Integer curSort) {
        return selectOne(Wrappers.<WikiCatalogDo>lambdaQuery()
                .eq(WikiCatalogDo::getWikiId, wikiId)
                .eq(WikiCatalogDo::getParentId, parentId)
                .isNotNull(WikiCatalogDo::getArticleId)
                .orderByAsc(WikiCatalogDo::getSort)
                .gt(WikiCatalogDo::getSort, curSort)
                .last("LIMIT 1"));
    }
}
